<?php declare(strict_types=1);
/**
 * @author      guosir
 * @copyright   2023-2025 owner
 **/

namespace Srv\Libs\Frame;

use Srv\Apps\Proto\VO\PageVO;

final class Page
{
    private int $currPage       = 0;            // 当前页码
    private int $totalNum       = 0;            // 记录总数
    private int $size           = 10;           // 每页显示数量

    /**
     * @param int $totalNum
     * @param int $pageSize
     * @param int $currPage
     * @return void
     * init
     */
    public function init(int $totalNum=0, int $pageSize=0, int $currPage=0):void
    {
        if($totalNum > 0) $this->totalNum = $totalNum;
        if($pageSize > 0) $this->size     = $pageSize;
        if($currPage > 0) $this->currPage = $currPage;
    }

    /**
     * @param string $key
     * @param $val
     * @return bool
     * 设置分页所需参数
     */
    public function setParam(string $key, $val):bool
    {
        switch($key){
            case 'size':{
                $num = intval($val);
                if($num > 0){
                    $this -> size = $num;
                }else{
                    return false;
                }
                break;
            }
            case 'currPage':{
                $num = intval($val);
                if($num > 0){
                    $this -> currPage = $num;
                }else{
                    return false;
                }
                break;
            }
            case 'totalNum':{
                $num = intval($val);
                if($num > 0) $this -> totalNum = $num;
                break;
            }
        }
        return TRUE;
    }

    /**
     * @return int
     * 获取当前第几页
     */
    public function getCurrPage():int
    {
        return max($this -> currPage, 1);
    }

    /**
     * @param int $currPage
     * @return array
     * 获取分页对应sql位置值
     */
    public function getLimit(int $currPage = 0):array
    {
        $currPage = $this -> getCurrPage();
        $currPage   = max($currPage, 1);
        $start      = ($currPage - 1) * $this -> size;
        return [$start, $this -> size];
    }

    /**
     * @return string
     * 获取分页对应sql的limit字符串
     */
    public function getLimitString():string
    {
        $currPage       = $this -> getCurrPage();
        $currPage       = max($currPage, 1);
        $start          = ($currPage - 1) * $this -> size;
        return ' LIMIT '.($start > 0 ? $start.', ' : '').$this -> size;
    }

    /**
     * @param int $totalNum
     * @param int $currPage
     * @return PageVO
     */
    public function getPageVO(int $totalNum = 0, int $currPage = 0):PageVO
    {
        $totalNum       = max($totalNum, 0);
        if($totalNum < 1) $totalNum = $this -> totalNum;
        if($currPage < 1) $currPage = $this -> getCurrPage();
        $totalPage      = $totalNum < 1 ? 1 : (ceil($totalNum/$this -> size));  // 总页数
        $nextNum        = min(($currPage + 1), $totalPage);        // 下一页
        $preNum         = max(($currPage - 1), 1);          // 上一页
        $PageVO         = new PageVO();
        $PageVO->setTotalSize($totalNum);
        $PageVO->setTotalPage($totalPage);
        $PageVO->setPageSize($this->size);
        $PageVO->setCurrPage($currPage);
        $PageVO->setNextPage($nextNum);
        $PageVO->setPrePage($preNum);
        return $PageVO;
    }
}